package com.apps.ppt.utils;

import com.apps.saspect.RunTimes;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;


import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

import java.io.FileInputStream;
import java.util.ArrayList;
import javax.imageio.ImageIO;

/**
 * pptx文件图片处理
 */
public class PPtxImagesHelper
{
    /**
     * 读取ppt幻灯片缩放比例
     */
    private static int read_images_rate=3;
    /**
     * 保存成图片缩放比例
     */
    private static float save_images_scale=0.3f;

    private static int row_space=2;

    /**
     * 读取PPT幻灯片,拼成一张图片
     * @param file
     * @return
     * @throws Exception
     */
    public static BufferedImage readPPtToImages(File file,int row) throws Exception
    {
        FileInputStream is = new FileInputStream(file);
        SlideShow ppt = SlideShowFactory.create(is);
        is.close();
        Dimension pageSize = ppt.getPageSize();
        //单页ppt的宽和高
        int width=pageSize.width*read_images_rate;
        int height = pageSize.height*read_images_rate;
        //ppt的页数,必须是偶数
        int len=calculationLenght(ppt.getSlides().size(),row);
        //创建图片缓存
        BufferedImage  result=new BufferedImage(width, ((len/row)*(height/row))+((len/row)*row_space), BufferedImage.TYPE_INT_RGB);
        for (int i=0;i< len;i++)
        {
            try {
                Slide slide= (Slide) ppt.getSlides().get(i);

                BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.BLUE);
                graphics.scale(read_images_rate, read_images_rate);
                graphics.fill(new Rectangle2D.Float(0, 0, width, height));
                slide.draw(graphics);
                //合成详情图
                Image imges=img.getScaledInstance((width/row),(height/row),Image.SCALE_DEFAULT);
                //求出拼接图片的X和Y坐标
                int x=(calculationX(i,row))*((width/row)+(row-1)*row_space);
                int y=((i/row)*(height/row))+((i/row)*row_space);
                //塞进画板
                result.getGraphics().drawImage(imges,x,y,null);
            }catch (Exception e)
            {
                e.printStackTrace();
                continue;
            }
        }

        return result;
    }

    /**
     * 计算数组的长度，长度必须是row的完整倍数
     * @param size
     * @param row
     * @return
     */
    public static int calculationLenght(int size,int row)
    {
        int result=size;
        while (size%row!=0)
               size--;
        return size;
    }

    /**
     * 求X轴的坐标
     * @param index
     * @param row
     * @return
     */
    public static int calculationX(int index,int row)
    {
        if(index%row == 0)
            return row-1;
        else
            return (index%row)-1;
    }
    public static void main(String... args)
    {
        for(int i=1;i<20;i++)
        System.out.println(3+"%"+i+":"+calculationX(i,3));
    }

//    /**
//     * 将图片拼接成PPT缩略图的形式并保存
//     * @param bufferedImages  图片数组
//     * @param pageSpace 拼装图片间距
//     * @param thumb_height  预览图高度
//     * @param path  保存路径
//     */
//    public static void PicturePuzzleByPPtxThumb(java.util.List<BufferedImage> bufferedImages, int pageSpace, int thumb_height, String path,String name)
//    {
//        thumb_height*=read_images_rate;
//        try {
//            if(bufferedImages.size() <=0 )
//                return;
//            int height = bufferedImages.get(0).getHeight();
//            int width = bufferedImages.get(0).getWidth();
//            //拼装图片
//            BufferedImage ThumbImg = new BufferedImage(width, (height + pageSpace)* bufferedImages.size(), BufferedImage.TYPE_INT_RGB);
//            //偶数计数器
//            int count=1;
//            //循环合成
//            for(int i=0;i<bufferedImages.size();i++)
//            {
//                BufferedImage imageBuffer = bufferedImages.get(i);
//
//                int[] imageRgbArray = new int[width * height];
//                imageRgbArray = imageBuffer.getRGB(0, 0, width, height, imageRgbArray, 0, width);
//                //第一张图片最大
//                if(i==0)
//                {
//                    ThumbImg.setRGB(0, (height  + pageSpace) * i, width, height, imageRgbArray, 0, width);
//                }
//                else
//                {
//                    if((i%2)==0)
//                        count++;
//                    //求出y
//                    int y=(count<=2?height:(height/2)*count)+pageSpace;
//                    //求出x
//                    int x=i%2==1?0:(width/2)+pageSpace;
//                    //缩放布局
//                    Image img=imageBuffer.getScaledInstance((width/2)-(pageSpace/2),(height/2)-pageSpace,Image.SCALE_DEFAULT);
//                    //塞进画板
//                    ThumbImg.getGraphics().drawImage(img,x,y,null);
//                }
//            }
//            BufferedImage result=ThumbImg.getHeight()>thumb_height?ThumbImg.getSubimage(0,0,ThumbImg.getWidth(),thumb_height):ThumbImg;
//            ImageIO.write(Thumbnails.of(result).scale(save_images_scale,save_images_scale).asBufferedImage(), "jpg", new File(path+name+".jpg"));
//            result = null;
//        }catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 生成并保存ppt详情图
//     * @param bufferedImages
//     * @param pageSpace
//     * @param path
//     */
//    public static void PicturePuzzleByPPtxDetail(java.util.List<BufferedImage> bufferedImages, int pageSpace, String path,String name)
//    {
//        try {
//            if(bufferedImages.size() <=0)
//                return;
//            int height = bufferedImages.get(0).getHeight();
//            int width = bufferedImages.get(0).getWidth();
//            //拼装图片
//            BufferedImage DetailImg = new BufferedImage(width, (height + pageSpace)* bufferedImages.size(), BufferedImage.TYPE_INT_RGB);
//
//            //偶数计数器
//            int count=1;
//            //循环合成
//            for(int i=0;i<bufferedImages.size();i++)
//            {
//                BufferedImage imageBuffer = bufferedImages.get(i);
//
//                int[] imageRgbArray = new int[width * height];
//                imageRgbArray = imageBuffer.getRGB(0, 0, width, height, imageRgbArray, 0, width);
//                //合成详情图
//                DetailImg.setRGB(0,(height  + pageSpace) * i,width,height,imageRgbArray,0,width);
//            }
//            ImageIO.write(Thumbnails.of(DetailImg).scale(save_images_scale,save_images_scale).asBufferedImage(), "jpg",new File(path+name+".jpg"));
//        }catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//    }


}
